import 'dart:convert';
import 'package:amway_superapp_creator_plugin/models/circle_detail_model.dart';
import 'package:amway_superapp_creator_plugin/models/create_user_info_model.dart';
import 'package:amway_superapp_creator_plugin/models/haowu_product_models.dart';
import 'package:amway_superapp_creator_plugin/utils/sp_util.dart';
import 'package:amway_superapp_creator_plugin/viewmodels/common_webview.dart';
import 'package:amway_superapp_creator_plugin/views/camera_page_test.dart';
import 'package:amway_superapp_creator_plugin/views/circle/multimedia_preview.dart';
import 'package:amway_superapp_creator_plugin/views/circle/my_circle.dart';
import 'package:amway_superapp_creator_plugin/views/circle/my_circle_details_images.dart';
import 'package:amway_superapp_creator_plugin/views/circle/my_circle_details_video.dart';
import 'package:amway_superapp_creator_plugin/views/circle/send_editer_page_view.dart';
import 'package:amway_superapp_creator_plugin/views/cover/set_cover_view.dart';
import 'package:amway_superapp_creator_plugin/views/create_splash_view.dart';
import 'package:amway_superapp_creator_plugin/views/edit/edit_data.dart';
import 'package:amway_superapp_creator_plugin/views/edit/look_image.dart';
import 'package:amway_superapp_creator_plugin/views/home/create_home.dart';
import 'package:amway_superapp_creator_plugin/views/home/home_guide_details.dart';
import 'package:amway_superapp_creator_plugin/views/home/home_guide_list_page.dart';
import 'package:amway_superapp_creator_plugin/views/image_bean.dart';
import 'package:amway_superapp_creator_plugin/views/login.dart';
import 'package:amway_superapp_creator_plugin/views/page_change_theme.dart';
import 'package:amway_superapp_creator_plugin/views/page_main_setting.dart';
import 'package:amway_superapp_creator_plugin/views/page_tag_list.dart';
import 'package:amway_superapp_creator_plugin/views/page_user_icon.dart';
import 'package:amway_superapp_creator_plugin/views/recommend/page_recommend_product.dart';
import 'package:amway_superapp_creator_plugin/views/selected/selected_combination.dart';
import 'package:amway_superapp_creator_plugin/views/shop/shop_category.dart';
import 'package:amway_superapp_creator_plugin/views/shop/shop_list.dart';
import 'package:amway_superapp_creator_plugin/views/test.dart';
import 'package:amway_superapp_creator_plugin/views/test_sel_photo_view.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:amway_superapp_creator_plugin/views/circle/official_circle_detail.dart';

var rootHandler = Handler(
    handlerFunc: (BuildContext context, Map<String, List<String>> params) {
  final args = context.settings.arguments as Map;
  var amwayAda = args['amwayAda'];
  var amwayId = args['amwayId'];
  var phone = args['phone'];

  return CreateHome(
    amwayAda: amwayAda,
    amwayId: amwayId,
    phone: phone,
  );
});

var guideListHandler = Handler(
    handlerFunc: (BuildContext context, Map<String, List<String>> params) {
  return HomeGuideListPage();
});

var testprovideHandler = Handler(
    handlerFunc: (BuildContext context, Map<String, List<String>> params) {
  return LoginView();
});

var testSelPhotoView = Handler(
    handlerFunc: (BuildContext context, Map<String, List<String>> params) {
  return TestSelPhotoView();
});

var changeThemeView = Handler(
    handlerFunc: (BuildContext context, Map<String, List<String>> params) {
  return ChangeThemePage();
});

var mainSettingsView = Handler(
    handlerFunc: (BuildContext context, Map<String, List<String>> params) {
  final args = context.settings.arguments as Map;

  var adaId = args['amwayAda'];
  var amwayId = args['amwayId'];
  var identityMarks = args['identityMarks']; //身份标签  官方圈子 需要

  return MainSettingPage(adaId, amwayId, identityMarks);
});

var tagListView = Handler(
    handlerFunc: (BuildContext context, Map<String, List<String>> params) {
  String pageType = params["pageType"].first;
  return TagListPage(pageType);
});

var userIconView = Handler(
    handlerFunc: (BuildContext context, Map<String, List<String>> params) {
  final icon = context.settings.arguments as String;
  return UserIconPage(icon);
});

var recommendProductView = Handler(
    handlerFunc: (BuildContext context, Map<String, List<String>> params) {
  final args = context.settings.arguments as Map;
  var adaId;
  var amwayId;
  if (args != null) {
    adaId = args['amwayAda'];
    amwayId = args['amwayId'];
  }
  return RecommendProductPage(ada: adaId, amwayId: amwayId);
});

var testPageView = Handler(
    handlerFunc: (BuildContext context, Map<String, List<String>> params) {
  return ReorderPage();
});

//设置封面
var setCoverViewHander = Handler(
    handlerFunc: (BuildContext context, Map<String, List<String>> params) {
  final args = context.settings.arguments as Map;

  var coverList = args['coverList'];
  var amwayAda = args['amwayAda'];

  return SetCoverView(
    covers: coverList,
    amwayAda: amwayAda,
  );
});

var editDataView = Handler(
    handlerFunc: (BuildContext context, Map<String, List<String>> params) {
  final args = context.settings.arguments as Map;
  final amwayAda = args['amwayAda'];
  var amwayId = args['amwayId'];
  var phone = args['phone'];
  var isFromLaunch = args['isFromLaunch'] ?? false;
  CreateUserInfoModel createUserInfoModel;
  var model = args['createUserInfoModel'];
  if (model == null) {
    createUserInfoModel = CreateUserInfoModel(
        amwayId: amwayId,
        ada: amwayAda,
        coverList: [],
        nick: "",
        introduce: "",
        interestMarks: [],
        identityMarks: [],
        wxCode: "",
        displayAudits: [],
        mobile: phone,
        kuaiShouCode: "",
        kuaiShouQRCode: ImagesBean(),
        douYinCode: "",
        douYinQRCode: ImagesBean(),
        wxEnterpriseQRCode: ImagesBean());
  } else {
    if (model is CreateUserInfoModel) {
      if (model.id == "" || model.id == null) {
        model.mobile = phone;
      }
    }
    createUserInfoModel = model;
  }
  if (createUserInfoModel.amwayId == null ||
      createUserInfoModel.amwayId.isEmpty) {
    createUserInfoModel.amwayId = amwayId;
  }
  // if (createUserInfoModel.mobile == null ||
  //     createUserInfoModel.mobile.isEmpty) {
  //   createUserInfoModel.mobile = phone;
  // }
  var hasDraft = args['hasDraft'] == null ? false : args['hasDraft'];
  print(amwayAda);
  SpUtil.setData("${SpUtil.local_user_data}$amwayAda",
      json.encode(createUserInfoModel)); //用于比较是否修改内容
  return EditDataPage(amwayAda,
      createUserInfoModel: createUserInfoModel, hasDraft: hasDraft);
});
var selectedCombinationView = Handler(
    handlerFunc: (BuildContext context, Map<String, List<String>> params) {
  return SelectedCombination();
});
//我的圈子
var myCircle = Handler(
    handlerFunc: (BuildContext context, Map<String, List<String>> params) {
  final args = context.settings.arguments as Map;
  var amwayAda = args['amwayAda'];
  var amwayId = args['amwayId'];
  var identityMarks = args['identityMarks'];

  return MyCircle(
    amwayAda: amwayAda,
    amwayId: amwayId,
    identityMarks: identityMarks,
  );
});

var commonWebView = Handler(
    handlerFunc: (BuildContext context, Map<String, List<String>> params) {
  final args = context.settings.arguments as Map;

  var url = args['url'];

  return CommonWebView(url);
});

var myCircleDetailsvideo = Handler(
    handlerFunc: (BuildContext context, Map<String, List<String>> params) {
  final args = context.settings.arguments as Map;
  var id = "";
  if (args != null) {
    id = args['id'];
  }
  return MyCircleDetailsVideo(id);
});
var myCircleDetailsImages = Handler(
    handlerFunc: (BuildContext context, Map<String, List<String>> params) {
  final args = context.settings.arguments as Map;
  var id = "";
  if (args != null) {
    id = args['id'];
  }
  return MyCircleDetailsImages(id);
});

var editerCircleTopView = Handler(
    handlerFunc: (BuildContext context, Map<String, List<String>> params) {
  final args = context.settings.arguments as Map;
  var isNewAdd = true;
  CircleDetailData circleData;
  bool isUserDraft = false;
  List<ImageBean> images = new List();
  var amwayId = "";
  var amwayAda = "";
  if (args != null) {
    isNewAdd = args['isNewAdd'];
    circleData = args['circleData'];
    isUserDraft = args['isUserDraft'];
    amwayId = args['amwayId'];
    amwayAda = args['amwayAda'];
    images = args['images'];
    if (circleData == null) {
      circleData = CircleDetailData(
          title: "",
          amwayId: amwayId,
          ada: amwayAda,
          description: "",
          officialFlag: "NO",
          titleVerify: 0,
          descriptionVerify: 0,
          medias: List<ImagesBean>());
    }
  } else {
    circleData = CircleDetailData(
        title: "",
        description: "",
        titleVerify: 0,
        descriptionVerify: 0,
        medias: List<ImagesBean>());
  }
  return SendEditerPageView(
    isNewAdd: isNewAdd,
    amwayAda: amwayAda,
    amwayId: amwayId,
    isUserDraft: isUserDraft,
    circleData: circleData,
    images: images,
  );
});

var sendEditerPage = Handler(
    handlerFunc: (BuildContext context, Map<String, List<String>> params) {
  final args = context.settings.arguments as Map;
  var isNewAdd = true;
  CircleDetailData circleData;
  bool isUserDraft = false;
  var amwayId = "";
  var amwayAda = "";
  List<ImageBean> images = [];
  if (args != null) {
    isNewAdd = args['isNewAdd'];
    circleData = args['circleData'];
    isUserDraft = args['isUserDraft'];
    amwayId = args['amwayId'];
    amwayAda = args['amwayAda'];
    images = args['images'];
    if (circleData == null) {
      circleData = CircleDetailData(
          title: "",
          amwayId: amwayId,
          ada: amwayAda,
          description: "",
          officialFlag: "NO",
          titleVerify: 0,
          descriptionVerify: 0,
          medias: List<ImagesBean>());
    }
  } else {
    circleData = CircleDetailData(
        title: "",
        description: "",
        titleVerify: 0,
        descriptionVerify: 0,
        medias: List<ImagesBean>());
  }
  return SendEditerPageView(
    isNewAdd: isNewAdd,
    amwayAda: amwayAda,
    amwayId: amwayId,
    isUserDraft: isUserDraft,
    circleData: circleData,
    images: images,
  );
});
//欢迎页
var createSplashView = Handler(
    handlerFunc: (BuildContext context, Map<String, List<String>> params) {
  final args = context.settings.arguments as Map;

  var amwayAda = args['amwayAda'];
  var amwayId = args['amwayId'];
  var phone = args['phone'];
  return CreateSplashView(
    amwayAda: amwayAda,
    amwayId: amwayId,
    phone: phone,
  );
});
var chooseShop = Handler(
    handlerFunc: (BuildContext context, Map<String, List<String>> params) {
  final args = context.arguments as Map;
  if (args != null) {
    List<HaowuProduct> list = args['items'];
    return ShopCategory(chosenList: list);
  } else {
    return ShopCategory();
  }
});

var chooseShopList = Handler(
    handlerFunc: (BuildContext context, Map<String, List<String>> params) {
  final args = context.settings.arguments as Map;
  var title = "";
  var code = "";
  List<HaowuProduct> list = [];
  if (args != null) {
    title = args['title'];
    code = args['code'];
    list = args['list'];
  }
  return ShopList(title, code, list);
});
var cameraTest = Handler(
    handlerFunc: (BuildContext context, Map<String, List<String>> params) {
  final args = context.settings.arguments as Map;
  if (args != null) {}
  return CameraExampleHome();
});

var lookImage = Handler(
    handlerFunc: (BuildContext context, Map<String, List<String>> params) {
  final args = context.settings.arguments as Map;
  String image = "";
  if (args != null) {
    image = args['image'];
  }
  return LookBigImage(image);
});

var multimeadiaPreview = Handler(
    handlerFunc: (BuildContext context, Map<String, List<String>> params) {
  final args = context.settings.arguments as Map;
  List images = args['imageList'];
  int index = args['index'];
  return MultimeadiaPreview(
    index: index,
    images: images,
  );
});

var guideDetails = Handler(
    handlerFunc: (BuildContext context, Map<String, List<String>> params) {
  final args = context.settings.arguments as Map;

  int type = args['type'];
  String dataHtml = args['dataHtml'];
  return HomeGuideDetails(
    dataHtml: dataHtml,
    type: type,);


});
var officialCircleDetail = Handler(
    handlerFunc: (BuildContext context, Map<String, List<String>> params) {
  final args = context.settings.arguments as Map;
  String dataHtml = args['dataHtml'];
  String time = args['time'];
  String title = args['title'];
  List<String> officialId = args['officialId'];
  List<ImagesBean> images = args['images'];
  return OfficialCircleDetail(
    dataHtml: dataHtml,
    time: time,
    title: title,
    officialId: officialId,
    images: images,
  );
});
